<?php

class PdoSituation extends PdoConnect {

    public function createSituation(situation $situation) {
        try {
            $this->open();
            $sql = "INSERT INTO `situation`(titre, descriptif) VALUES(?, ?)";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $situation->getTitre(), PDO::PARAM_STR);
            $prep->bindValue(2, $situation->getDescription(), PDO::PARAM_STR);

            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readAllSituation() {
        try {
            $this->open();
            $sql = "SELECT * From `situation`";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function deleteSituation(situation $situation) {

        try {
            $this->open();
            $sql = "DELETE FROM situation WHERE id=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $situation->getID(), PDO::PARAM_INT);
            $prep->execute();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function updateSituation(situation $situation) {

        $this->open();
        $sql = "UPDATE situation SET titre = ?, descriptif= ? WHERE id=?";

        $prep = $this->pdo->prepare($sql);

        $prep->bindValue(1, $situation->getTitre(), PDO::PARAM_STR);
        $prep->bindValue(2, $situation->getDescription(), PDO::PARAM_STR);
        $prep->bindValue(3, $situation->getID(), PDO::PARAM_INT);

        $prep->execute();
    }

    public function existSituation($titre) {
        $this->open();
        $query = "SELECT * FROM situation WHERE titre='$titre';";
        $statement = $this->pdo->query($query);
        return $statement->rowCount();
    }

}
